package com.sendi.system.util.common;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/*
 * 日志记录类
 */
public class LogDebug {
	
	/*
	 * 默认的日志记录路径
	 */
	private static String DEFAULT_LOG="/usr/local/tomcat/logs/Wanglanglog.txt";
	
	private static HashMap<String,FileWriter> logers=new HashMap<String,FileWriter> ();
	
	
	/*
	 * 默认的日志记录写文件类
	 */
	private static FileWriter Default_logger;
	
	static
	{
		try {
			Default_logger=new FileWriter(DEFAULT_LOG,true);
		} catch (IOException e) {
		 
			e.printStackTrace();
			System.out.println("������־�ļ����:"+e.getMessage());
		}
	}
	
	public static void Log(String path,String msg)
	{
		FileWriter fw=logers.get(path);
		if(fw==null)
		{
			try {
				fw=new FileWriter(path,true);
				logers.put(path, fw);
			} catch (IOException e) {
				 
				e.printStackTrace();
			}
		}
		
		try {
			 
			fw.append(new Date().toLocaleString()+" :"+msg);
			fw.append("\n\r");
			fw.flush();
			 
			
		} catch (IOException e) {
		 
			e.printStackTrace();
			 
		}
	}
	
	/*
	 * 是否加入堆栈调用信息
	 */
	public static void Log(String path,String msg,boolean isStackTrace)
	{
		FileWriter fw=logers.get(path);
		if(fw==null)
		{
			try {
				fw=new FileWriter(path,true);
				logers.put(path, fw);
			} catch (IOException e) {
				 
				e.printStackTrace();
			}
		}
		
		try {
			 
			fw.append(new Date().toLocaleString()+" :"+msg);
			fw.append("\n\r");
			if(isStackTrace)
			{
				fw.append("-------------------------------堆栈调用信息--------------------------\r\n");
				StackTraceElement stack[] = (new Throwable()).getStackTrace();   
				 
				int ix = 0;   
				while (ix < stack.length) {   
					fw.append("\n\r");
				StackTraceElement frame = stack[ix];   
			 	fw.append("类名:"+frame.getClassName()+"\tMethodName:"+frame.getMethodName()+"\tLineNumber"+frame.getLineNumber()) ;
			 	ix++;
				}
				fw.append("\r\n-------------------------------堆栈调用信息-----------------------");
				fw.append("\n\r");
			}
			fw.flush();
			 
			
		} catch (IOException e) {
		 
			e.printStackTrace();
			 
		}
	}
	
	
	public static void getInvokeName()
	{
		
	 	StackTraceElement stack[] = (new Throwable()).getStackTrace();   
		 
		int ix = 0;   
		while (ix < stack.length) {   
		StackTraceElement frame = stack[ix];   
		String cname = frame.getClassName();   
		System.out.println(cname);
		System.out.println(frame.getMethodName());
		System.out.println(frame.getLineNumber());
		}
	}
	public static void Log(String msg)
	{
		try {
			 
			Default_logger.append(new Date().toLocaleString()+" :"+msg);
			Default_logger.append("\n\r");
			Default_logger.flush();
			 
			
		} catch (IOException e) {
		 
			e.printStackTrace();
			 
		}
		
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	 
		
		Thread t=new Thread(){
			
			public void run()
			{
				for(int i=0;i<100;i++)
				{
					System.out.println("线程1执行.....");
					LogDebug.Log("d:\\a.txt","asf");
					try {
						Thread.currentThread().sleep(1000);
					} catch (InterruptedException e) {
					 
						e.printStackTrace();
					}
				}
			}
			
		};
		
		Thread t2=new Thread(){
			
			public void run()
			{
				for(int i=0;i<1000;i++)
				{
					System.out.println("线程2执行.....");
					LogDebug.Log("d:\\a.txt","!!!!!!!!!!!!!!!!!!!!!!!!asf",true);
					try {
						Thread.currentThread().sleep(1000);
					} catch (InterruptedException e) {
					 
						e.printStackTrace();
					}
				}
			}
			
		};
		t.start();
		t2.start();
	}

}
